• The SKTilemap class is a mappable container for managing layers of tiles (sprites), vector objects & images. Tile data is stored in SKTileset tile sets.

    Usage

    Maps can be loaded with the class function SKTilemap.load(tmxFile:):

    if let tilemap = SKTilemap.load(tmxFile: "MyFile.tmx") {
        scene.addChild(tilemap)
    }
    

    Properties

    • mapSize: Size of the map (in tiles).
    • tileSize: Map tile size (in pixels).
    • sizeInPoints: Size of the map in pixels.
    • orientation: Map orientation (orthogonal, isometric, etc).
    • boundingRect: Map bounding rect.
    • tilesets: Array of stored tileset instances.
    • allowZoom: Allow camera zooming.
    • layers: Array of child layers.

    For more information, see the Working with Maps page in the official documentation.

    See more

    Declaration

    Swift

    public class SKTilemap : SKNode, CustomReflectable, TiledMappableGeometryType, TiledObjectType
  • The SKTileset class manages a set of SKTilesetData objects, which store tile data including global id, texture and animation.

    Tileset Setup

    Tile data is accessed via a local id, and tiles can be instantiated with the resulting SKTilesetData instance:

    if let data = tileset.getTileData(localID: 56) {
       let tile = SKTile(data: data)
    }
    

    Properties

    • name: tileset name.
    • tilemap: reference to parent tilemap.
    • tileSize: tile size (in pixels).
    • localRange: range of local tile data id values.
    • globalRange: range of global tile data id values.
    • columns: number of columns.
    • tilecount: tile count.
    • firstGID: first tile global id.
    • lastGID: last tile global id.
    • tileData: set of tile data structures.

    Class Functions

    • load(tsxFile:delegate:): Load a tileset from a file.
    • load(tsxFiles:delegate:): Load multiple tilesets.

    Instance Methods

    • addTextures(): Generate textures from a spritesheet image.
    • addTilesetTile(): Add & return new tile data object.

    For more information, see the Working with Tilesets page in the official documentation.

    See more

    Declaration

    Swift

    public class SKTileset : NSObject, CustomReflectable, TiledObjectType
  • The TiledLayerObject is the base class for all SKTiled layer types.

    This class doesn’t specify any object or child types, but provides base behaviors for layered content, including:

    • coordinate transformations
    • validating coordinates
    • positioning and alignment

    Properties

    • tilemap: the parent tilemap container.
    • index: layer index. Matches the index of the layer in the source TMX file.
    • mapSize: layer size (in tiles).
    • tileSize: layer tile size (in pixels).
    • anchorPoint: layer anchor point, used to position layers.
    • offset: offset value for tweaking layer position. Matches the layer offset values set in the source TMX file.

    Instance Methods

    • pointForCoordinate(coord:offset:): returns a point for a coordinate in the layer, with optional offset.
    • coordinateForPoint(_:): returns a tile coordinate for a given point in the layer.
    • touchLocation(_:): returns a converted touch location in map space. (iOS only)
    • coordinateAtTouchLocation(_:): returns the tile coordinate at a touch location. (iOS only)
    • isValid(coord:): returns true if the coordinate is valid.

    Usage

    All layer types share identical methods for translating screen coordinates to map coordinates (and vice versa):

    // assign a position matching a coordinate in the current projection
    node.position = tileLayer.pointForCoordinate(2, 1)
    
    // translate a point to map a coordinate
    coord = coordinateForPoint(touchPosition)
    

    In addition, all layer types respond to mouse/touch events:

    // return the tile coordinate at a touch event (iOS)
    coord = imageLayer.coordinateAtTouchLocation(touchPosition)
    
    // return the tile coordinate at a mouse event (macOS)
    coord = groupLayer.coordinateAtMouse(event: mouseClicked)
    
    See more

    Declaration

    Swift

    public class TiledLayerObject : SKEffectNode, CustomReflectable, TiledMappableGeometryType, TiledObjectType
  • Subclass of TiledLayerObject, the tile layer is a container for an array of tiles (sprites). Tiles maintain a link to the map’s tileset via their SKTilesetData property.

    Properties

    • tileCount: returns a count of valid tiles.

    Instance Methods

    • getTiles(): returns an array of current tiles
    • getTiles(ofType:): returns tiles of the given type
    • getTiles(globalID:): returns all tiles matching a global id
    • getTilesWithProperty(_:_): returns tiles matching the given property & value
    • animatedTiles(): returns all animated tiles
    • getTileData(globalID:): returns all tiles matching a global id
    • tileAt(coord:): returns a tile at the given coordinate, if one exists

    Usage

    Accessing a tile at a given coordinate:

    let tile = tileLayer.tileAt(2, 6)!
    

    Query tiles of a certain type:

    let floorTiles = tileLayer.getTiles(ofType: "Floor")
    
    See more

    Declaration

    Swift

    public class SKTileLayer : TiledLayerObject
  • The SKObjectGroup class is a container for vector object types. Most object properties can be set on the parent SKObjectGroup which is then applied to all child objects.

    Properties

    • count: returns the number of objects in the layer.
    • showObjects: toggle visibility for all of the objects in the layer.
    • lineWidth: governs object line width for each object.
    • debugDrawOptions: debugging display flags.

    Methods

    • addObject: add an object to the object group.
    • removeObject: remove an object from the object group.
    • getObject(withID:): returns an object with the given id, if it exists.

    Usage

    Adding a child object with optional color override:

    objectGroup.addObject(myObject, withColor: SKColor.red)
    

    Querying an object with a specific name:

    let doorObject = objectGroup.getObject(named: "Door")
    

    Returning objects of a certain type:

    let rockObjects = objectGroup.getObjects(ofType: "Rock")
    
    See more

    Declaration

    Swift

    public class SKObjectGroup : TiledLayerObject
  • Subclass of TiledLayerObject, the SKGroupLayer node is a container for managing groups of layers.

    Usage

    Query child layers:

    for child in group.layers {
        child.opacity = 0.5
    }
    

    Add layers to the group with:

    groupLayer.addLayer(playerLayer)
    

    Remove with:

    groupLayer.removeLayer(playerLayer)
    
    See more

    Declaration

    Swift

    public class SKGroupLayer : TiledLayerObject
  • The SKImageLayer object is really nothing more than a sprite with positioning attributes.

    Properties

    • image: Layer image name.
    • wrapX: Wrap horizontally.
    • wrapY: Wrap vertically.

    Methods

    • setLayerImage: set the layer’s image.
    • setLayerTexture: set the layer’s texture.
    • wrapY: wrap vertically.

    Usage

    Set the layer image with:

    imageLayer.setLayerImage("clouds-background")
    
    See more

    Declaration

    Swift

    public class SKImageLayer : TiledLayerObject
  • The SKTile class is a custom SpriteKit sprite node that references its image and animation data from a tileset container. The tile represents a single piece of a larger image stored in a tile layer container.

    Tile Data Setup

    Properties

    • globalId: tile global id.
    • tileData: tileset tile data reference.
    • tileSize: tile size (in pixels).
    • layer: parent tile layer.

    Instance Methods

    • setupPhysics(shapeOf:isDynamic:): setup physics for the tile.
    • setupPhysics(rectSize:isDynamic:): setup physics for the tile.
    • setupPhysics(withSize:isDynamic:): setup physics for the tile.
    • runAnimation(): play tile animation (if animated).
    • removeAnimation(restore:): remove animation.
    • runAnimationAsActions(): runs a SpriteKit action to animate the tile.
    • removeAnimationActions(restore:): remove the animation for the current tile.
    See more

    Declaration

    Swift

    open class SKTile : SKSpriteNode, CustomReflectable
  • The SKTilesetData structure stores data for a single tileset tile, referencing the tile texture or animation frames (for animated tiles).

    This class optionally includes navigation properties for tile accessability, and graph node weight.

    Properties

    • id: tile id (local).
    • type: tiled object type.
    • texture: Tile texture.
    • tileOffset: Tile offset.
    See more

    Declaration

    Swift

    public class SKTilesetData : CustomReflectable, TiledObjectType
  • The SKTileObject class represents a Tiled vector object type (rectangle, ellipse, polygon, point & polyline). When the object is created, points can be added either with an array of points, or a string. In order to render the object, the SKTileObject.getVertices() method is called, which returns the points needed to draw the path.

    Properties

    • id: Tiled object id.
    • size: object size.
    • tileData: tile data (for tile objects).
    • text: text string (for text objects). Setting this redraws the object.
    • bounds: returns the bounding box of the shape.

    For more information, see the Working with Objects page in the [official documentation][sktiled-docroot-url].

    See more

    Declaration

    Swift

    open class SKTileObject : SKShapeNode, CustomReflectable, TiledObjectType
  • The SKTiledScene object represents a scene of content in SpriteKit, customized for including Tiled asset types. This scene type automatically creates camera and world container nodes and sets up interactivity between them and an associated tile map node.

    SKTiledScene Hierarchy

    The camera node determines what part of the scene’s coordinate space is visible in the view.

    Properties

    • worldNode: Root container node.
    • tilemap: Tile map node.
    • cameraNode: Custom scene camera.

    Instance Methods

    • cameraPositionChanged: called when the camera position changes.
    • cameraZoomChanged: called when the camera zoom changes.
    • cameraBoundsChanged: called when the camera bounds updated.

    Instance Methods (iOS)

    • sceneDoubleTapped: Called when the scene receives a double-tap event (iOS only).

    Instance Methods (macOS)

    • sceneClicked: called when the scene is clicked (macOS only).
    • sceneDoubleClicked: called when the scene is double-clicked (macOS only).
    • mousePositionChanged: called when the mouse moves in the scene (macOS only).

    See also

    For more information, see the Setting Up Your Scenes page in the official documentation.

    See more

    Declaration

    Swift

    open class SKTiledScene : SKScene, SKPhysicsContactDelegate, TiledSceneDelegate, TilemapDelegate, TilesetDataSource
    extension SKTiledScene: TiledSceneCameraDelegate
  • The SKTiledSceneCamera is a custom scene camera that responds to finger gestures and mouse events.

    Camera Hierarchy

    This node is a custom camera meant to be used with a scene conforming to the TiledSceneDelegate protocol. The camera defines a position in the scene to render the scene from, with a reference to the TiledSceneDelegate.worldNode to interact with tile maps.

    Properties

    • world: world container node.
    • delegates: array of delegates to notify about camera updates.
    • zoom: camera zoom value.
    • allowMovement: toggle to allow camera movement.
    • minZoom: minimum zoom value.
    • maxZoom: maximum zoom value.
    • zoomClamping: clamping factor used to alleviate render artifacts like cracking.

    For more information, see the Tiled Scene Camera page in the official documentation.

    See more

    Declaration

    Swift

    public class SKTiledSceneCamera : SKCameraNode
    extension SKTiledSceneCamera: TiledCustomReflectableType
  • The SKTiledGraphNode node is a custom GKGridGraphNode object that adds a weight parameter for use with Tiled scene properties. Can be used with normal GKGridGraphNode instances. The SKTiledGraphNode.weight property is used to affect the estimated cost to a connected node. (Increasing the weight makes it less likely to be travelled to, decreasing more likely).

    Usage

    // query a node in the graph and increase the weight property
    if let node = graph.node(atGridPosition: coord) as? SKTiledGraphNode {
       node.weight = 25.0
    }
    
    See more

    Declaration

    Swift

    public class SKTiledGraphNode : GKGridGraphNode, TiledObjectType
  • The TileAnimationFrame structure represents a single frame of animation. Time is stored in milliseconds.

    Properties

    • id: unique tile (local) id.
    • duration: frame duration.
    • texture: optional tile texture.
    See more

    Declaration

    Swift

    public class TileAnimationFrame : NSObject
  • The TileFlags optionset represents the various transformation flags that can be set for a given tile.

    Properties

    • none: tile is rendered with no transformations.
    • flipHorizontal: tile is flipped on the x-axis.
    • flipVertical: tile is flipped on the y-axis.
    • flipDiagonal: tile is rotated.
    See more

    Declaration

    Swift

    public struct TileFlags : OptionSet
  • The TileIDstructure provides an interface to a masked tile id.

    Tile flipping is represented by a mask of a tile global id, with the upper three bits storing orientation values (flipped horizontally, flipped vertically, flipped diagonally).

    Usage

    This structure is not meant to be used directly; simply passing a value to the SKTile.tileId property will set the global id and orientation of the tile.

    See the Working with Tiles documentation for more information.

    // A raw value of 2147483659 translates to 11, flipped horizontally.
    let gid: UInt32 = 2147483659
    var tileId = TileID(wrappedValue: 2147483659)
    print(tileId)
    // Tile ID: 11, flags: [ hFlip ]
    
    // Alternately, we can set the flags directly:
    tileId.flags = [.all]
    print(tileId)
    // Tile ID: 11, flags: [ hFlip, vFlip, dFlip ]
    
    See more

    Declaration

    Swift

    @propertyWrapper
    public struct TileID